package com.jcfk.eam.service.csm;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.core.utils.BeanUtils;
import com.jcfk.common.security.utils.SecurityUtils;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.eam.dao.csm.CsmWorkRecordDao;
import com.jcfk.eam.domain.dto.csm.CsmWorkRecordDTO;
import com.jcfk.eam.domain.po.csm.CsmWorkRecord;
import com.jcfk.eam.domain.query.csm.CsmWorkRecordQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

/**
 * 耗材操作记录查询
 *
 * @author yrl
 * @email 
 * @date 2024-11-05
 */
@Service
public class CsmWorkRecordService extends BaseService<CsmWorkRecordDao, CsmWorkRecord> {

    /**
     * CsmWorkRecord分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<CsmWorkRecordDTO> queryByPage(DataPage<CsmWorkRecordDTO> page, CsmWorkRecordQuery query) {
        // 分页查询
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


        /**
     * 判断CsmWorkRecord是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(CsmWorkRecordDTO dto) {
        QueryWrapper<CsmWorkRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);
                                                            
        return RetResult.ok();
    }

    /**
     * 插入耗材操作记录查询
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(CsmWorkRecordDTO dto) {

        // 保存
        CsmWorkRecord item = new CsmWorkRecord();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新耗材操作记录查询
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(CsmWorkRecordDTO dto) {

        // 保存
        CsmWorkRecord item = new CsmWorkRecord();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }

    /**
     * 根据批量主键删除耗材操作记录查询(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(CsmWorkRecordDTO dto) {

        CsmWorkRecord item = new CsmWorkRecord();

        this.setDefaultValue(item);
        item.setDeleted("1");

        QueryWrapper<CsmWorkRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(CsmWorkRecord item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
                item.setDeleted("0");
                                                                            item.setCreatedUserName(SecurityUtils.getUserName());
                                                                                                                                            }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

                                    item.setLastUpdatedUserName(SecurityUtils.getUserName());
                                    
    }

    /**
     * 根据主键获取耗材操作记录查询信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public CsmWorkRecordDTO get(String id) {
        // 查询数据
        CsmWorkRecord item = this.baseMapper.selectById(id);
            CsmWorkRecordDTO dto = new CsmWorkRecordDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }

    

    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<CsmWorkRecordDTO>
     */
    public List<CsmWorkRecordDTO> getList(CsmWorkRecordQuery query) {
        // 查询条件
        QueryWrapper<CsmWorkRecord> qw = new QueryWrapper<>();
                                                                                                                                                                                                                                                                                                                                        qw.lambda().eq(!StrUtil.isEmpty(query.getMaterialId()), CsmWorkRecord::getMaterialId, query.getMaterialId());
                                                    qw.lambda().eq(!StrUtil.isEmpty(query.getWorkType()), CsmWorkRecord::getWorkType, query.getWorkType());
                                                    qw.lambda().eq(!StrUtil.isEmpty(query.getWorkStatus()), CsmWorkRecord::getWorkStatus, query.getWorkStatus());
                                                                                                                            // 查询数据
        List<CsmWorkRecord> list = super.list(qw);

        return convertList(list, CsmWorkRecordDTO.class);
    }

    /**
     * 根据主键获取耗材操作记录查询信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteItem(String id) {
            CsmWorkRecord item = getById(id);
        this.setDefaultValue(item);
        item.setDeleted("1");

        this.updateById(item);
        }
}

